$(function () {
    //常用jQuery对象
    var entity_datagrid = $("#entity_list");
    var entity_dailog = $("#entity_dialog");
    var entity_form = $("#entity_form");


    //==========================================================================
    //封装方法
    var methodObj = {
        //添加按钮方法
        add: function () {
            $("#entity_form input").prop("disabled",false);
            entity_form.form("clear");
            entity_dailog.dialog("open");
            entity_dailog.dialog("setTitle", "大客户新增");
        },

        //编辑按钮方法
        edit: function () {
            var row = entity_datagrid.datagrid("getSelected");
            if (!row) {
                $.messager.alert("温馨提示", "亲!请至少选择一条数据", "info");
                return;
            }
            //  //回显重要性
            if(row.importance){
                row["importance.id"] = row.importance.id;
            }
            //回显意向程度
            if(row.wantedLevel){
                row["wantedLevel.id"] = row.wantedLevel.id;
            }
            //回显意向学科
            if(row.subject){
                row["subject.id"] = row.subject.id;
            }
            //回显意向学区
            if(row.college){
                row["college.id"] = row.college.id;
            }
            //回显学校联系人
            if(row.contact){
                row["contact.id"] = row.contact.id;
            }
            //回显营销人员
            if(row.marketer){
                row["marketer.id"] = row.marketer.id;
            }
            //回显跟进人员
            if(row.tracer){
                row["tracer.id"] = row.tracer.id;
            }
            //回显学校类型
            if(row.type){
                row["type.id"] = row.type.id;
            }



            entity_dailog.dialog("open");
            entity_dailog.dialog("setTitle", "编辑");
            //回显列表数据
            entity_dailog.form("load", row);

        },


        //编辑或新增方法具体逻辑
        saveOrUpdate: function () {
            entity_form.form("submit", {
                url: "/university/saveOrUpdate.do",
                success: function (data) {
                    data = $.parseJSON(data);
                    if (data.success) {
                        $.messager.alert("温馨提示", data.msg, "info", function () {
                            entity_dailog.dialog("close");
                            entity_datagrid.datagrid("reload");
                        });
                    } else {
                        $.messager.alert("温馨提示", data.msg, "info", function () {
                            entity_dailog.dialog("close");
                        });
                    }
                }
            })
        },

        delete: function () {
            var row = entity_datagrid.datagrid("getSelected");
            if (!row) {
                $.messager.alert("温馨提示", "亲!请至少选择一条数据", "info");
                return;
            }
            $.messager.confirm('温馨提示',"请问确定要删除此角色吗?", function (r) {
                if (r) {
                    $.get("/university/delete.do", {id: row.id}, function (data) {
                        if (data.success) {
                            $.messager.alert("温馨提示", data.msg, "info", function () {
                                entity_datagrid.datagrid("reload");
                            });
                        } else {
                            $.messager.alert("温馨提示", data.msg, "info");
                        }
                    }, "json")
                }
            });

        },


        //设置大客户状态
        changState: function () {
            var row = entity_datagrid.datagrid("getSelected");
            if (!row) {
                $.messager.alert("温馨提示", "亲!请至少选择一条数据", "info");
                return;
            }
            var entity_state = "亲,要签约？";
            if (row.customerStatus) {
                var entity_state = "亲,取消签约？";
            }
            $.messager.confirm('温馨提示', entity_state, function (r) {
                if (r) {
                    $.get("/university/changState.do", {id: row.id}, function (data) {
                        if (data.success) {
                            $.messager.alert("温馨提示", data.msg, "info", function () {
                                entity_datagrid.datagrid("reload");
                            });
                        } else {
                            $.messager.alert("温馨提示", data.msg, "info");
                        }
                    }, "json")
                }
            });

        },

        //刷新页面
        reloadList: function () {
            entity_datagrid.datagrid("reload");
        },
        //查看
        see: function () {
            var row = entity_datagrid.datagrid("getSelected");
            if (!row) {
                $.messager.alert("温馨提示", "亲!请至少选择一条数据", "info");
                return;
            }
            //  //回显重要性
            if(row.importance){
                row["importance.id"] = row.importance.id;
            }
            //回显意向程度
            if(row.wantedLevel){
                row["wantedLevel.id"] = row.wantedLevel.id;
            }
            //回显意向学科
            if(row.subject){
                row["subject.id"] = row.subject.id;
            }
            //回显意向学区
            if(row.college){
                row["college.id"] = row.college.id;
            }
            //回显学校联系人
            if(row.contact){
                row["contact.id"] = row.contact.id;
            }
            //回显营销人员
            if(row.marketer){
                row["marketer.id"] = row.marketer.id;
            }
            //回显跟进人员
            if(row.tracer){
                row["tracer.id"] = row.tracer.id;
            }
            //回显学校类型
            if(row.type){
                row["type.id"] = row.type.id;
            }

            $("#entity_form input").prop("disabled",true);

            entity_dailog.dialog("open");
            entity_dailog.dialog("setTitle", "编辑");
            //回显列表数据
            entity_dailog.form("load", row);

        },
    }


    //==========================================================================================
    //按钮绑定事件
    $("[data-cmd]").click(function () {
        var cmd = $(this).data("cmd");
        methodObj[cmd]();
    });

    //页面数据加载
    entity_datagrid.datagrid({
        fit: true,
        fitColumns: true,
        striped: true,
        url: "/university/query.do",
        pagination: true,
        singleSelect: true,
        nowrap: false,
        toolbar: "#tools",
        onClickRow: function (index, row) {
            if (row.customerStatus){
                $("#btn_changeState").linkbutton({
                    text: "取消签约"
                });
                $("a[data-cmd='edit']").linkbutton("disable");//编辑 disable
            } else {
                $("#btn_changeState").linkbutton({
                    text: "签约"
                });
                $("a[data-cmd='edit']").linkbutton("enable");
            }

        },
        columns: [[
            {field: 'id', title: '编号', width: 80, align: 'center'},
            {field: 'name', title: '学校', width: 80, align: 'center'},
            {field: 'address', title: '地址', width: 80, align: 'center'},
            {
                field: 'importance',
                title: '重要度',
                width: 100,
                align: 'center',
                formatter: function (value, row, index) {
                    return value ? value.name : "";
                }
            },
            {
                field: 'wantedLevel',
                title: '意向度',
                width: 80,
                align: 'center',
                formatter: function (value, row, index) {
                    return value ? value.name : "";
                }
            },
            {field: 'schoolTel', title: '学校电话', width: 100, align: 'center'},
            {
                field: 'subject', title: '意向学科', width: 80, align: 'center', formatter: function (value, row, index) {
                    return value ? value.name : "";
                }
            },
            {
                field: 'contact', title: '联系人', width: 80, align: 'center', formatter: function (value, row, index) {
                    return value ? value.name : "";
                }
            },
            {
                field: 'marketer', title: '营销人员', width: 80, align: 'center', formatter: function (value, row, index) {
                    return value ? value.username : "";
                }
            },
            {
                field: 'tracer', title: '跟进人员', width: 80, align: 'center', formatter: function (value, row, index) {
                    return value ? value.username : "";
                }
            },
            {field: 'prevTraceTime', title: '上次跟进时间', width: 150, align: 'center'},
            {field: 'nextTraceTime', title: '下次跟进时间', width: 150, align: 'center'},
            {
                field: 'traceState',
                title: '跟进状态',
                width: 100,
                align: 'center',
                formatter: function (value, row, index) {
                    if (value == null) {
                        return "";
                    } else {
                        return value ? "已跟进" : "<font color='red'>待跟进</font>";
                    }
                }
            },
            {
                field: 'customerStatus',
                title: '客户状态',
                width: 100,
                align: 'center',
                formatter: function (value, row, index) {
                    if (value == null) {
                        return "";
                    } else {
                        return value ? "<font color='green'>已签约</font>" : "<font color='red'>未签约</font>";
                    }
                }
            },
        ]],


    });

    //编辑添加弹出框设置
    entity_dailog.dialog({
        width: 1000,
        height: 600,
        closed: true,
        buttons: [{
            text: '保存',
            iconCls: "icon-save",
            handler: methodObj["saveOrUpdate"]
        }, {
            iconCls: "icon-cancel",
            text: '取消',
            handler: function () {
                entity_dailog.dialog("close");
            }
        }]
    });



    //form表单设置
    entity_dailog.form({
        fit: true
    });

});

